#include "types.h"
#include "err.h"

typedef struct flist
{
    struct flist *next;
    uint8_t data[0];
}flist_t;
    
static inline err_t create_flist(uint8_t *ptr, uint16_t node_size, uint16_t count, flist_t **freelist)
{
    flist_t *flist = NULL;
    flist_t *nptr = NULL;
    uint16_t i = 0;

    if (!ptr || !node_size || !count) {
        return ERR_INVALID;
    }

    flist = (flist_t *)ptr;
    nptr = flist;
    for (i = 0; i < count; i++) {
        flist = nptr;
        nptr = (flist_t *)((uint8_t *)flist + node_size);
	flist->next = nptr;
    }

    flist->next = NULL;

    *freelist = (flist_t *)ptr;
    return ERR_SUCCESS;
}

static inline flist_t *pop_flist(flist_t **freelist)
{
    flist_t *flist = *freelist;
    if ((*freelist)) {
	*freelist = (*freelist)->next;
    }

    return flist;
}

static inline err_t push_flist(flist_t **freelist, flist_t *node)
{
    if (*freelist) {
        node->next = *freelist;
    } else {
        node->next = NULL;
    }

    *freelist = node;
    return ERR_SUCCESS;
}

